﻿namespace WMS6.Web.GlobalControl
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class WarehouseTransfer : BasePage
    {
        protected Button btnGoBack;
        protected Button btnRefresh;
        protected DropDownList ddlDestWhDBName;
        protected DropDownList ddlSrcWhDBName;
        protected HtmlForm F13512;
        protected GridView gvIntransitDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnIntransitID;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnSrcDbID;
        protected Label lblActualShipDate;
        protected Label lblAllocateRule;
        protected Label lblDestWhDBName;
        protected Label lblExpectedReceiptDate;
        protected Label lblIntransitID;
        protected Label lblMsg;
        protected Label lblOrderDate;
        protected Label lblOrderID;
        protected Label lblOwnerID;
        protected Label lblReceiptID;
        protected Label lblSrcWhDBName;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtActualShipDate;
        protected TextBox txtExpectedReceiptDate;
        protected TextBox txtIntransitID;
        protected TextBox txtOrderDate;
        protected TextBox txtOrderID;
        protected TextBox txtOwnerID;
        protected TextBox txtReceiptID;
        protected UpdatePanel up1;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("WarehouseTransferList.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("WarehouseTransferList.aspx?from=edit", false);
                    break;
            }
        }

        protected string FormatIntransitDetailUrl(string url, string IntransitID, string LineID, string SrcDbID)
        {
            return string.Format(url, IntransitID, LineID, SrcDbID);
        }

        protected void gvIntransitDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as IntransitInfo).IntransitDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvIntransitDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
            }
        }

        protected void gvIntransitDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                IntransitInfo intransitInfo = base.DataEntityViewState as IntransitInfo;
                DataView dv = CADataConverter.ToDataTable(intransitInfo.IntransitDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvIntransitDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void LoadData()
        {
            base.BindData2DDL4WareHouse(this.ddlDestWhDBName);
            base.BindData2DDL4WareHouse(this.ddlSrcWhDBName);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnIntransitID.Value = base.Request.QueryString["ID"].ToString();
                if (!string.IsNullOrEmpty(base.Request.QueryString["SrcDbID"]))
                {
                    this.hdnSrcDbID.Value = base.Request.QueryString["SrcDbID"].ToString();
                }
                this.SetReceiptInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.InitPage(this.Page);
            this.LoadData();
        }

        private void SetReceiptInfo2Form()
        {
            try
            {
                string IntransitID = this.hdnIntransitID.Value.ToString();
                string srcDbID = this.hdnSrcDbID.Value.ToString();
                IntransitInfo intransitInfo = OutboundServiceClientProxy.GetInstance().GetIntransitInfoByID(IntransitID, srcDbID);
                base.DataEntityViewState = intransitInfo;
                if (intransitInfo != null)
                {
                    this.txtIntransitID.Text = intransitInfo.IntransitID;
                    this.txtOrderID.Text = intransitInfo.OrderID;
                    this.txtReceiptID.Text = intransitInfo.ReceiptID;
                    this.ddlDestWhDBName.SelectedValue = intransitInfo.DestWhDbID.ToString();
                    this.ddlSrcWhDBName.SelectedValue = intransitInfo.SrcWhDbID.ToString();
                    this.txtOwnerID.Text = intransitInfo.OwnerID;
                    this.txtOrderDate.Text = base.GetPreferenceShortDate(intransitInfo.OrderDate);
                    this.txtActualShipDate.Text = base.GetPreferenceShortDate(intransitInfo.ActualShipDate);
                    this.txtExpectedReceiptDate.Text = base.GetPreferenceShortDate(intransitInfo.ExpectedReceiptDate);
                    if ((intransitInfo.IntransitDetailInfos != null) && (intransitInfo.IntransitDetailInfos.Count > 0))
                    {
                        this.hdnLineID.Value = (intransitInfo.IntransitDetailInfos[intransitInfo.IntransitDetailInfos.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvIntransitDetail, intransitInfo.IntransitDetailInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

